//	$p_delete = $obj_UserValidation->check_Permission(permissions\cPermissions::C_STR_PERMISION_SAVED_QUERY_DELETE);
//	$p_edit = $obj_UserValidation->check_Permission(permissions\cPermissions::C_STR_PERMISION_SAVED_QUERY_EDIT);

//var m_cnt_SavedQueryStore = Ext.create('AmpleHarvest.Data.Store.cManager');

Ext.define('AmpleHarvest.objects.aqb.manager.cManager', {
    extend: 'Ext.panel.Panel',
	alias			: 'widget.ampleharvest-objects-aqb-manager-cmanager',
    requires		:
    [
     	'Ext.grid.Panel'
     ],    
    title: 'Query Manager',
    width:700,
    
 
    deleteQuery: function(obj_Btn)
    {
    	if(obj_Btn == 'yes')
    	{    		
	      	var conn = new Ext.data.Connection();
	      	conn.request(
	      	{
	      	    url: obj_config.C_STR_EXTERNAL_PATH+'/savedquery/deleteQuery.php',
	      	    method: 'POST',
	      	    params: 
	      	    {
	      	    	"ID": AmpleHarvest.enhancedSearch.forms.manager.cManager.m_str_DeleteID
	      	    },
	      	    success: function(responseObject) 
	      	    {
	    	      	lastOptions = m_cnt_SavedQueryStore.lastOptions;
	    			m_cnt_SavedQueryStore.reload(lastOptions);  
	      	        Ext.MessageBox.alert(
	      				"Success",
	      				"Your query has been removed");
	      		},
	      	    failure: function() 
	      	    {
	      	        Ext.MessageBox.alert(
	      				"Failure",
	      				"Something went wrong!");
	      	    }
	      	});
    	}
    },

    initComponent: function()
    {
    	var obj_This = this;

		var m_cnt_SavedQueryStore = Ext.create('AmpleHarvest.DATA.Store.cManager');
    	
    	var m_str_DeleteID = '';

    	var obj_Date = new Date;
    	
    	this.m_obj_QueryManager = Ext.create('Ext.grid.Panel', {
    		viewConfig: {
    			forceFit : true,
    			disableSelection : true
    		},
        	id : obj_Date.getTime() + "_GridView",
        	store : m_cnt_SavedQueryStore,
        	columns: [{
    	        	  xtype			: 'actioncolumn',
    		        	  width			: 65,
    		        	  fixed			: true,
    		        	  menuDisabled	: true,
    		        	  items			: [

 //   		if ($p_delete)
 //   		{

    		        	      {
    		        		  iconCls	: 'query-delete',
    	        		  tooltip	: 'Delete Query',
    	        		  handler	: function(grid, rowIndex, colIndex){
    				    	  			var rec = m_cnt_SavedQueryStore.getAt(rowIndex);
    				    	  			//alert(rec.data.q_title);
    				    	  			//Ext.AmpleHarvest.QueryManager.Grid.m_str_DeleteID = rec.data.id;
    				    	  			AmpleHarvest.enhancedSearch.forms.manager.cManager.m_str_DeleteID = rec.data.id;
    				    	  			Ext.MessageBox.confirm(
    				    						"Confirm Delete",
    				    						"Are you sure you want to delete \""+rec.data.q_title+"\"?",
    				    						obj_This.deleteQuery);
    					      				}
    		        	  	  },

 //   		}
 //   		if ($p_edit)
 //   		{

    		        	  	  {
    		        	  	  iconCls	: 'query-edit',
    	        	  	  tooltip	: 'Load Query',
    	        	  	  handler	: function(grid, rowIndex, colIndex){
    	        	  		  			var rec = m_cnt_SavedQueryStore.getAt(rowIndex);
    	        	  		  				open_Window(0,"",rec.data.id);
    					     				}
    		        	  	  }, 

  //  		}

    						  {
    		        	  	  iconCls	: 'query-load',
    	        	  	  tooltip	: 'Edit Query',
    	        	  	  handler	: function(grid, rowIndex, colIndex)
    	        	  	  {	  
    	        	  		var obj_Form = document.createElement("form");
    	  		  			var rec = m_cnt_SavedQueryStore.getAt(rowIndex);
    	  		  			var obj_Input = null;
    	        	  		  
    	  		  			obj_Form.method="post" ;
    	  		  			
    	  		  			obj_Form.action = obj_config.C_STR_ENHANCED_SEARCH_PATH;
    	  		  			
//    		  		  		switch(rec.data.sourcename)
//    	  		  			{
//        	  		  			case 'Reaper':
//        	  		  				obj_Form.action = obj_config.C_STR_REAPER_SEARCH_PATH;
//        	  		  				break;
//        	  		  			case 'OctoberSky':
//        	  		  				obj_Form.action = obj_config.C_STR_OCTOBERSKY_SEARCH_PATH;
//        	  		  				break;
//        	  		  			case 'GroundEffect':
//        	  		  				obj_Form.action = obj_config.C_STR_GROUNDEFFECT_SEARCH_PATH;
//        	  		  				break;
//        	  		  			default:
//        	  		  				obj_Form.action = obj_config.C_STR_REAPER_SEARCH_PATH;
//    	  		  			}
    		  		  		
    		  		  		// Set Submit
    		  		  		obj_Input = document.createElement("input") ;
    		  		  		obj_Input.setAttribute("name", "json") ;
    		  		  		obj_Input.setAttribute("value", rec.data.q_string);
    		  		  		obj_Form.appendChild(obj_Input) ;
    	        	  		 
    		  		  		document.body.appendChild(obj_Form) ;
    		  		  		obj_Form.submit() ;
    	        	  		document.body.removeChild(obj_Form) ;
    	  				}
    	        	  	  
    	        	  }],
                  },
                  {
                      header   : 'Title', 
                      sortable : true, 
                      dataIndex: 'q_title'
                  },
                  {
                      header   : 'Schedule Structure (H,D,DOTW,Y)',
                      sortable : false, 
                      dataIndex: 'cron'
                  },
                  {
                      header   : 'Last Ran', 
                      sortable : true, 
                      dataIndex: 'last_run'
                  },
                  {
                      header   : 'Next Run', 
                      sortable : true, 
                      dataIndex: 'next_run'
                  },
                  {
                      header   : 'Source', 
                      sortable : true, 
                      dataIndex: 'sourcename'
                  },
              ],
        	tools : [
     				{
     					type : 'refresh',
     					on :
     					{
     						click : function()
     						{
     							grid.body.mask('Loading', 'x-mask-loading'); 											
     							lastOptions = m_cnt_SavedQueryStore.lastOptions;
     							m_cnt_SavedQueryStore.reload(lastOptions);
     							setTimeout(function()
     							{ // mimic a server call
     										grid.body.unmask();
     									}, 500); 							
     						}
     					}
     				} ],
        	applyTo : "Window",
        	height:900,
        	stripeRows: true,
        	title: 'Query Manager',
          });
        

    	this.items = 
    	[
	    	this.m_obj_QueryManager
    	]
    	this.callParent();
    }
});

